<?php
/**
 * 控制器
 * @author 41412754@qq.com
 */
class control {
	
	public $app;
	public $group;
	public $config;
	
	public $module;
	public $action;
	
	public $get;
	public $post;
	public $request;
	
	public $validate;
	
	public $model;
	
	public function __construct( $automated = false )
	{
		$this->app = &$GLOBALS['app'];
		$this->group = &$this->app->group;
		$this->config = &$this->app->config;
		$this->module = &$this->app->module;
		$this->action = &$this->app->action;
		$this->router = &$this->app->router;
		
		$this->get = &$_GET;
		$this->post = &$_POST;
		$this->request = &$_REQUEST;
		$this->session = &$_SESSION;
		$this->cookie = &$_COOKIE;
		
		$this->validate = &$this->app->validate;
		
		$this->loadModel();
	}
	
	/**
	 * 方法重载
	 * @link http://my.oschina.net/u/726339/blog/80352
	 */
	public function __call( $name, $arguments )
	{
		trigger_error( "$name() 方法不存在" );
	}
	
	/**
	 * 加载模型
	 * @param $model
	 * @param $quiet 可选
	 */
	public function loadModel( $model = null )
	{
		$modelName = $model . 'Model';
		if ( $model === null ) $modelName = $this->module . $modelName;
		if ( ! class_exists( $modelName ) )
		{
			$modelFile = $model === null ? "../module/{$this->module}/$modelName.class.php" : "../module/$model/$modelName.class.php";
			$exists = file_exists( $modelFile );
			if ( $model !== null and ! $exists ) trigger_error( "模型文件 $modelFile 不存在" );
			if ( $exists ) require $modelFile;
		}
		else
			$exists = true;
		if ( $exists )
		{
			$this->$modelName = new $modelName();
			if ( $model === null ) $this->model = $this->$modelName;
			return $this->$modelName;
		}
	}
	
	/**
	 * 加截类
	 * @param $className
	 * @param $static 可选
	 */
	public function loadClass( $className, $static = false )
	{
		return $this->$className = $this->app->loadClass( $className, $static );
	}
	
	/**
	 * 加载小工具
	 * $param $widgetName
	 */
	public function loadWidget( $widgetName )
	{
		$widget = $widgetName;
		$widgetName .= 'Widget';
		if ( ! class_exists( $widgetName ) )
		{
			$widgetFile = "../widget/$widget/$widgetName.class.php"; // 自定义类
			if ( ! file_exists( $widgetFile ) ) trigger_error( "小工具文件 $className.class.php 不存在" );
			require $widgetFile;
		}
		
		if ( ! is_object( $widgetName ) ) $this->$widgetName = new $widgetName();
		$this->$widgetName->widgetName = $widget;
		return $this->$widgetName;
	}
	
	/**
	 * 显示模板
	 * @param $viewFile 可选
	 */
	public function display( $act = null, $mod = null )
	{
		if ( $act === null ) $act = $this->action;
		if ( $mod === null ) $mod = $this->module;
		
		$viewFile = "../view/{$this->group}/$mod/$act.html.php";
		if ( ! file_exists( $viewFile ) ) trigger_error( "视图文件 $viewFile 不存在" );
		extract( (array)$this->view );
		require $viewFile;
	}
	
	/**
	 * 默认操作
	 */
	public function index()
	{
		echo '<div style="font-family:微软雅黑; font-size:100px; color:#333; text-align:center;">: )</div>';
	}
}
